package com.ruoyi.web.controller.yada;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.system.domain_yada.*;
import com.ruoyi.system.service_yada.IAorestCoverageService;
import com.ruoyi.system.service_yada.IAustraliaMiddleEastService;
import com.ruoyi.system.service_yada.ThematicMapService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.InputStream;
import java.security.Guard;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.List;

/**
 *  森林覆盖率
 */
@RestController
@RequestMapping(value = "/business-service/api/Aorest", produces = "application/json;charset=UTF-8")
@Api(tags = "森林覆盖率")
public class AorestCoverageController {
    @Resource
    private IAorestCoverageService service;

    @Resource
    private ThematicMapService thematicMapService;

    /**
     * 查询方法
     * 需要查询的字段参数
     * @return 参数类
     */
    @RequestMapping(value = "/selectAorestCoverage",method = {RequestMethod.GET})
    @ApiOperation(value = "查询方法",httpMethod = "GET")
    public AjaxResult selectAorestCoverage()
    {
        List<AorestCoverageVO> eastVOSList= service.selectAorestCoverage();
        return AjaxResult.success(eastVOSList);
    }

    /**
     * 删除方法
     * @param ID id
     * @return 状态码
     */
    @RequestMapping(value = "/DelAorestCoverage",method = {RequestMethod.DELETE})
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
    @ApiOperation(value = "删除方法",httpMethod = "DELETE")
    public AjaxResult DelAorestCoverage(@Param("ID") String ID)
    {
        int NUM = service.DelAorestCoverage(ID);
        if (NUM>0)
        {
            return AjaxResult.success();
        }
        return  AjaxResult.error();

    }

    /**
     * 新增方法
     * @param eastVOS 新增实体类
     * @return 状态码
     */
    @RequestMapping(value = "/IntoAorestCoverage",method = {RequestMethod.POST})
    @ApiOperation(value = "新增方法",httpMethod = "POST")
    public AjaxResult IntoAorestCoverage(@RequestBody AorestCoverageVO eastVOS)
    {
        try {


            eastVOS.setCreateTime(LocalDateTime.now());
            int NUM =  service.IntoAorestCoverage(eastVOS);
            if (NUM>0)
            {
                return AjaxResult.success();
            }
            return  AjaxResult.error();
        }
        catch (Exception E){
             E.getMessage();
            return  AjaxResult.error();
        }
    }

    /**
     * 上传文件
     * @return 状态码
     */
    @RequestMapping(value = "/IntoUpLoad",method = {RequestMethod.POST})
    @ApiOperation(value = "上传文件",httpMethod = "POST")
    public  AjaxResult IntoUpload(@RequestBody MultipartFile file)
    {
        UploadFile upload=new UploadFile();

        ThematicMapDomain domain=new ThematicMapDomain();

        UploadFile value= null;
        try {
            value = uploadFile(file,upload);
        } catch (Exception e) {
            e.printStackTrace();
        }

        int NUM =  service.IntoUpload(value);
        domain.setPictureCode(value.getFileId());

        if (NUM>0)
        {
            return AjaxResult.success();
        }
        return  AjaxResult.error();
    }


    /**
     * 文件查询
     * @param response 头
     * @param request 客户端请求
     * @return
     */
    @RequestMapping(value = "/selectUpload",method = {RequestMethod.GET})
    @ApiOperation(value = "文件查询",httpMethod = "GET")
    public AjaxResult selectUpload(HttpServletResponse response, HttpServletRequest request,@RequestBody UploadFile upload)
    {
        List<UploadFile> eastVOSList= service.selectUpload(upload.getFileId(),upload.getFileName());
        return AjaxResult.success(eastVOSList);
    }




    public UploadFile uploadFile(MultipartFile file,UploadFile upload) throws Exception
    {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        upload.setFileId(String.valueOf(System.currentTimeMillis()));
        upload.setFileName(file.getOriginalFilename());
        upload.setFilePath(fileName);
        upload.setFileSize(String.valueOf(file.getSize()));
        upload.setCreateTime(LocalDateTime.now());
        return  upload;
    }

    @RequestMapping(value = "/Dictionary")
    @ApiOperation(value = "字典查询",httpMethod = "POST")
     public  AjaxResult Dictionary(@RequestBody Dictionary dic)
     {
        List<Dictionary> diclist=service.selectDic(dic.getCodingType(),dic.getCodingType1(), dic.getCodingType2());
         return AjaxResult.success(diclist);
     }



}
